﻿using System;
using System.Data;
using System.Data.Linq;
using System.Linq;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using DevExpress.XtraReports.UI;
using Lists;

namespace ReceivingDelivering.Receiving
{
    public partial class Report : DevExpress.XtraReports.UI.XtraReport
    {
        DBDataContext db = new DBDataContext(GeneralLibs.LocalSettings.ConnectString);
        public Report(Lists.ReceivingBill voucher)
        {
            InitializeComponent();
            Config objCompanyName = db.Configs.FirstOrDefault(p => p.Name == "CompanyName");
            if (objCompanyName != null)
            {
                xrCompany.Text = objCompanyName.Value;
            }
            Config objAddress = db.Configs.FirstOrDefault(p => p.Name == "CompanyAddress");
            if (objAddress != null)
            {
                xrAddress.Text = objAddress.Value;
            }
            if (voucher == null) return;
            xrDate.Text = "Ngày " + voucher.ReceivingCreateDate.Value.ToString("dd/MM/yyyy");
            xrVoucherNo.Text = "Số: " + voucher.ReceivingNo;
            Staff objStaff = db.Staffs.FirstOrDefault(p => p.StaffID == voucher.StaffID);
            if (objStaff != null)
            {
                xrStaff.Text = objStaff.StaffName;
                xrStaffAddress.Text = objStaff.StaffAddress;
            }
            xrReason.Text = voucher.Description;

            //Đổ dữ liệu cho phần chi tiết
            var detail = db.ReceivingBillInfos.Join(db.Products,
                                                outer => outer.ProductID,
                                                inner => inner.ProductID,
                                                (outer, inner) =>
                                                    new
                                                    {
                                                        outer.ReceivingID,
                                                        inner.ProductNo,
                                                        ProductName = inner.ProductName,
                                                        inner.Unit,
                                                        outer.WarehouseID,
                                                        Quantity = outer.ReceivingQuantity,
                                                        UnitPrice = inner.ReceivingPrice,
                                                        ToMoney = inner .ReceivingPrice * outer.ReceivingQuantity
                                                    }
                                                    ).Where(p => p.ReceivingID == voucher.ReceivingID);
            if (detail != null)
            {
                this.DataSource = detail;
                xrCellProductID.DataBindings.Add("Text", DataSource, "ProductNo");
                xrCellProductName.DataBindings.Add("Text", DataSource, "ProductName");
                xrCellUnit.DataBindings.Add("Text", DataSource, "Unit");
                xrCellWareHouse.DataBindings.Add("Text", DataSource, "WarehouseID");
                xrCellQuantity.DataBindings.Add("Text", DataSource, "Quantity");
                xrCellUnitPrice.DataBindings.Add("Text", DataSource, "UnitPrice", "{0:#.00}");
                xrCellToMoney.DataBindings.Add("Text", DataSource, "ToMoney", "{0:#.00}");
                decimal? totalMoney = detail.Sum(p=>p.ToMoney);
                if (totalMoney != null)
                {
                    xrTotalMoney.Text = totalMoney.Value.ToString();
                    xrReadMoney.Text = "Số tiền bằng chữ: " + TienTe.ReadMoney(totalMoney.Value, "");
                }
            }
        }

    }
}
